const { ccclass, property } = cc._decorator;

@ccclass
export default class TarItem extends cc.Component {

    @property(cc.SpriteAtlas)
    blockSp: cc.SpriteAtlas = null;
    @property(cc.Node)
    blockBg: cc.Node = null;

    @property(cc.Label)
    num: cc.Label = null

    @property(cc.Node)
    okIcon: cc.Node = null;

    type = null;
    _num = 0;

    isOk = false;

    init(type, num) {
        this.type = type;
        this._num = num;
        this.num.string = String(num)
        switch (type) {
            case 1:
                this.blockBg.getComponent(cc.Sprite).spriteFrame = this.blockSp.getSpriteFrame('block_red')
                this.node.addComponent(cc.ParticleSystem);
                break;
            case 2:
                this.blockBg.getComponent(cc.Sprite).spriteFrame = this.blockSp.getSpriteFrame('block_green')
                break;
            case 3:
                this.blockBg.getComponent(cc.Sprite).spriteFrame = this.blockSp.getSpriteFrame('block_blue')
                break;
            case 4:
                this.blockBg.getComponent(cc.Sprite).spriteFrame = this.blockSp.getSpriteFrame('block_yellow')
                break;
            case 5:
                this.blockBg.getComponent(cc.Sprite).spriteFrame = this.blockSp.getSpriteFrame('block_purple')
                break;
        }

        // 播放特效

        this.node.runAction(cc.sequence(
            cc.scaleTo(0.15, 0.9),
            cc.scaleTo(0.15, 1)
        ));

    }

    addTile() {
        if (!this.isOk) {
            this._num--;
            if (this._num == 0) {
                this.isOk = true;
                this.okIcon.active = true;
                this.num.node.active = false;
            } else {
                this.blockBg.runAction(cc.sequence(
                    cc.scaleTo(0.2, 0.8),
                    cc.scaleTo(0.2, 1),
                    cc.scaleTo(0.2, 1.2),
                    cc.scaleTo(0.2, 1)
                ));
                this.num.string = String(this._num)
            }
        }
    }

}
